﻿/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Random;

import bean.User;
import util.JdbcUtils;


public class userDao {
	public static boolean insertUser(User user) {
		String sql = "insert into myuser values(?,?,?,?,?,?,?);";
		/*随机产生两位数*/
		Random ran = new Random();
		int nums = ran.nextInt(100);
		/*利用时间戳得到8位不重复的随机数*/
		Date date = new Date();
		long nowDate = date.getTime();
		/*将上面产生的随机数拼接成用户id*/
		String id = Integer.toHexString((int)nowDate) + Integer.toString(nums);
		user.setId(id);
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try{
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,user.getId());
			pstmt.setString(2,user.getName());
			pstmt.setString(3,user.getPsw());
			pstmt.setInt(4,user.getAge());
			pstmt.setString(5,user.getPhone());
			pstmt.setString(6,user.getAddress());
			pstmt.setString(7,user.getCity());
			int row = pstmt.executeUpdate();
			if(row == 1){
				return true;
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return false;
	}
	public static String verifyUser(String username,String psw) {
		String sql = "select id from myuser where name=? and psw=?;";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, psw);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return rs.getString("id");
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return "";
	}
	public static User queryUser(String id) {
		String sql = "SELECT * FROM myuser WHERE id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		User user = new User();
		user.setId(id);
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				user.setName(rs.getString("name"));
				user.setPsw(rs.getString("psw"));
				user.setAge(rs.getInt("age"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setCity(rs.getString("city"));
				return user;
				
			}
			rs.close();
			pstmt.close();
			conn.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static boolean updatepsw(String id,String psw) {
		String sql = "UPDATE myuser SET psw=? WHERE id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		int row = 0;
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, psw);
			pstmt.setString(2, id);
			row = pstmt.executeUpdate();
			if(row>0) {
				return true;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		return false;
	}
	
	
	public static boolean updateinfo(User user) {
		String sql = "update myuser set name=?,age=?,phone=?,address=?,city=? where id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		int row = 0;
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getName());
			pstmt.setInt(2, user.getAge());
			pstmt.setString(3, user.getPhone());
			pstmt.setString(4, user.getAddress());
			pstmt.setString(5, user.getCity());
			pstmt.setString(6, user.getId());
			row = pstmt.executeUpdate();
			if(row>0) {
				return true;
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
}
